/**
 * JMPlurk, a Java Mobile client for Plurk
 * Copyright (C) 2009 Petra Barus
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package com.maleskoding.jmplurk.ui;

import com.maleskoding.jmplurk.JMPlurk;
import com.maleskoding.jmplurk.plurkapi.PlurkException;
import com.maleskoding.jmplurk.ui.components.QualifierCombo;
import com.sun.lwuit.ComboBox;
import com.sun.lwuit.Command;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
import java.io.IOException;

/**
 *
 * @author Petra Barus
 */
public class NewPlurkForm extends Form implements ActionListener {

    private JMPlurk parent;
    //
    private Command postCommand;
    private Command cancelCommand;
    private ComboBox qualifierCombo;
    private TextArea contentField;

    /**
     *
     */
    public NewPlurkForm(JMPlurk parent) {
        super("New Plurk");
        this.parent = parent;
        init();
    }

    /**
     *
     */
    public void init() {
        this.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        qualifierCombo = new QualifierCombo();
        contentField = new TextArea("test posting from JMPlurk!", 5, 20);
        this.addComponent(qualifierCombo);
        this.addComponent(contentField);
        //
        postCommand = new Command("Post");
        cancelCommand = new Command("Back");
        this.addCommand(cancelCommand);
        this.addCommand(postCommand);
        this.addCommandListener(this);
    }

    /**
     *
     * @return
     */
    public Command getCancelCommand() {
        return cancelCommand;
    }

    /**
     *
     * @return
     */
    public Command getPostCommand() {
        return postCommand;
    }

    /**
     *
     */
    public void postPlurk() throws PlurkException, IOException {
        String qualifier = (String) qualifierCombo.getSelectedItem();
        //Label labelQualifier = (Label) qualifierCombo.getSelectedItem();
        //String qualifier = labelQualifier.getText();
        String content = contentField.getText();
        //NOTE: The user already logged in at Main Form
        //TODO: Set JMPlurk listener to this
        JMPlurk.plurk.addPlurk(content, qualifier);
    }

    public void actionPerformed(ActionEvent ae) {
        Command command = ae.getCommand();
        if (command == cancelCommand) {
            parent.getMainPlurkForm().show();
        } else if (command == postCommand) {
            try {
                postPlurk();
                parent.getMainPlurkForm().show();
            } catch (PlurkException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}
